﻿<?php
//header("Content-type:text/html;charset=utf-8");
/*
 * author:cao 2017-07-25
 * 重要的事说三遍:
 * 外部数据进行数据库操作请使用PDO预处理
 *
 */
class Logdb{

    private $pdo;
    private $res;
    private $config;

//构造函数
    function __construct(){
        $this->config = $this->Dbconn_config();
        try{
            $this->pdo = new PDO("mysql:host=".$this->config['host'].";dbname=".$this->config['dbname'].";port=".$this->config['port'],$this->config['username'],$this->config['password']);
            $this->pdo->exec("set names ".$this->config['charset']);
        }catch(Exception $exmsg){
            echo $exmsg->getmessage();
        }
    }


    private function Dbconn_config(){
        $config = array(
            'host'=>'rm-bp1t290ct1u7zjx7t.mysql.rds.aliyuncs.com',
            'dbname'=>'ugc_im',
            'port'=>'3306',
            'charset'=>'utf8',
            'username'=>'ugc_im',
            'password'=>'Ugc_imim'
        );

        return $config;
    }

//数据库关闭
    protected function close(){
        $this->pdo = null;
    }



//数据查询
    protected function query($sqlstr,$values=null){
        $query = $this->pdo->prepare($sqlstr);
        if(!is_array($values)){
            if(empty($values)){
                $ret = $query->execute();
            }else {
                return false;
            }
        }else{
            $ret = $query->execute($values);
        }

        if($ret){
            $rows = $query->fetchall(PDO::FETCH_OBJ);
            if($rows){
                return $rows;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }


//数据执行
    protected function commit($sqlstr,$values=null){
        $commit = $this->pdo->prepare($sqlstr);
        if(is_array($values) && count($values)>0){
            $ret = $commit->execute($values);
        }else{
            $ret = $commit->execute();
        }
        return $ret;
    }

   //验证是否为客服人员
    public function is_site_service($token){
        $sqlstr = "select `id`,`token`,`site_id` from chat_site_service_relation where `token`=:token";
        $info = $this->query($sqlstr,['token'=>$token]);
        if($info){
            return $info[0];
        }
        return false;
    }

   //绑定token到线程ID
   public function binding_token_connnection_id($token,$uid,$connection_id,$site_id,$nickname){

       $sqlstr = "select * from chat_user_connection where `token`=:token limit 1";
       $check = $this->query($sqlstr,['token'=>$token]);
       if($check){
           //更新
           $connection_info = $check[0];
           if($connection_info->connection_id==$connection_id){
               return true;
           }else{
               $sqlstr = "update chat_user_connection set `connection_id`=:connection_id,`uid`=:uid,`site_id`=:site_id,`nickname`=:nickname where `token`=:token";
               $this->commit($sqlstr,['connection_id'=>$connection_id,'uid'=>$uid,'site_id'=>$site_id,'nickname'=>$nickname,'token'=>$token]);
           }
       }else{
           //增加
           $sqlstr = "insert into chat_user_connection (`token`,`uid`,`connection_id`,`site_id`,`nickname`) VALUES (:token,:uid,:connection_id,:site_id,:nickname)";
           $this->commit($sqlstr,['token'=>$token,'uid'=>$uid,'connection_id'=>$connection_id,'site_id'=>$site_id,'nickname'=>$nickname]);

       }
          return true;

   }

    public function destroy_token_connnection_id($connection_id){
        //直接删除即可
        $sqlstr = "delete from chat_user_connection where `connection_id`=:connection_id";
        $result = $this->commit($sqlstr,['connection_id'=>$connection_id]);
        return $result;
    }

   // 查询客服是否在线

    public function get_site_service_status($site_id){

      $service = $this->get_site_service_id($site_id);
      if($service) {
          $token = $service->token;
          $connection_id = $this->get_user_status($token);
          if($connection_id){
              return $connection_id;
          }
      }

       return false;

    }

    public function get_client_info($client_uid){

        $sqlstr = "select * from chat_user_connection where `uid`=:uid limit 1";
        $info = $this->query($sqlstr,['uid'=>$client_uid]);
        if($info){
            return $info[0];
        }
        return false;

    }
    //查询用户是否在线
    public function get_user_status($token){

      $sqlstr = "select * from chat_user_connection where `token`=:token limit 1";
      $info = $this->query($sqlstr,['token'=>$token]);
      if($info){
          return $info[0]->connection_id;
      }
      return false;

    }




   public function get_site_service_id($site_id){
           $sqlstr = "select `id`,`token` from chat_site_service_relation where site_id=:site_id";
           $info = $this->query($sqlstr,['site_id'=>$site_id]);
           if($info){
               return $info[0];
           }
           return false;
   }


    //保存聊天记录
    public function save_log($data){
        $savedata['site_id'] = $data['site_id'];
        $savedata['uid'] = $data['uid'];
        $service_info = $this->get_site_service_id($data['site_id']);
        $savedata['to_service_id'] = $service_info->id;
        $savedata['event'] = $data['event'];
        $savedata['content'] = $data['content'];

       $sql = "insert into chat_user_log (`site_id`,`uid`,`to_service_id`,`event`,`content`) VALUES (:site_id,:uid,:to_service_id,:event,:content)";
       //$rs = $pdo->execute($sql,array('user_name'=>'mememe10002211','user_id'=>6));
        $this->commit($sql,$savedata);
        return true;
    }


    //获取谁与谁的聊天记录
    public function save_run_log(){

        $sql = "update chat_run_log set `create_time`=:create_time,`run_log`=:run_log where `key`='last'";
        $this->commit($sql,['create_time'=>date("Y-m-d H:i:s"),'run_log'=>'workerman starting,database connection ok']);
        return true;
    }






}



?>

